class EventsController < ApplicationController
  protect_from_forgery :only => [:create, :update, :destroy]
    layout :choose_layout
  # GET /events
  # GET /events.xml
  def index

 @events= Event.find_by_sql("SELECT e.id,e.name as eventName, e.winner, e.date_ini, e.date_fin, c.name as categoryName,a.name as administratorName FROM events e, categories c, administrators a WHERE e.category_id=c.id and e.administrator_id =a.id")
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @events }
    end
  end

  # GET /events/1
  # GET /events/1.xml

  def show
    @event = Event.find(params[:id])
    @category = Category.find(@event.category_id)
    @administrator= Administrator.find(@event.administrator_id)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
    end
  end

    def show_client
    @event = Event.find(params[:id])
    @category = Category.find(@event.category_id)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/new
  # GET /events/new.xml
  def new

    if params[:category_type]!=nil
      session[:category_type]=params[:category_type]
    end

    @event = Event.new

    @categories=Category.all.map{|x| x.name}

    2.times {@event.competitors.build} #no se necesita en el edit
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @event }
    end
  end

  # GET /events/1/edit
  def edit
    @event = Event.find(params[:id])
    @category = Category.find(@event.category_id)
    @administrator= Administrator.find(@event.administrator_id)
  end

  # POST /events
  # POST /events.xml
  def create

    @category=Category.first(:conditions => ["name=?",params[:category_id]])


    if @category
      params[:event][:category_id]=@category.id
    end

    @event = Event.new(params[:event])

    respond_to do |format|
      if @event.save
        flash[:notice] = t('flash.createEvent')
        logger.info '//////EL EVENTO HA SIDO CREADO EXITOSAMENTE////// '+Time.now.to_s
        format.html { redirect_to(@event) }
        format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
        logger.info '//////EL EVENTO NO PUDO SER CREADO////// '+Time.now.to_s
        @categories=Category.all.map{|x| x.name}
        format.html { render :action => "new" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.xml
  def update
    @event = Event.find(params[:id])


#    @category=Category.first(:conditions => ["name=?",params[:category_id]])
#
#
#    if @category
#      params[:event][:category_id]=@category.id
#    end


    respond_to do |format|
      if @event.update_attributes(params[:event])
        flash[:notice] = t('flash.updateEvent')
        logger.info '//////EL EVENTO FUE ACTUALZIDO EXITOSAMENTE////// '+Time.now.to_s
        format.html { redirect_to(@event) }
        format.xml  { head :ok }
      else
        @categories=Category.all.map{|x| x.name}
        format.html { render :action => "edit" }
        format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
        logger.info '//////NO SE PUDO ACTUALIZAR EL EVENTO////// '+Time.now.to_s
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.xml
  def destroy
    @event = Event.find(params[:id])
    @event.destroy

    respond_to do |format|
      format.html { redirect_to(events_url) }
      format.xml  { head :ok }
    end
  end

  

  private

  def choose_layout
    case action_name
    when "show_client"
      "template"
    else
      "templateAdmin"
    end
  end


end
